package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbClueTrackRecordMapper clueTrackRecordMapper;

    /**
     * 添加线索跟进记录
     * @param tbClueTrackRecord
     */
    @Override
    @Transactional
    public void addClueTrackRecord(ClueTrackRecordVo tbClueTrackRecord) {
        //创建线索跟进记录对象
        TbClueTrackRecord clueTrackRecord = new TbClueTrackRecord();

        //复制数据
        BeanUtils.copyBeanProp(clueTrackRecord,tbClueTrackRecord);

        //封装创建时间
        clueTrackRecord.setCreateTime(DateUtils.getDateHm());

        //封装创建人
        clueTrackRecord.setCreateBy(SecurityUtils.getUsername());

        //创建线索对象
        TbClue clue = new TbClue();
        //复制数据
        BeanUtils.copyBeanProp(clue,tbClueTrackRecord);

        //封装修改时间
        clue.setUpdateTime(DateUtils.getNowDate());

        //设置线索id
        clue.setId(tbClueTrackRecord.getClueId());

        //设置状态
        clue.setStatus(TbClue.StatusType.FOLLOWING.getValue());

        //修改线索
        clueMapper.updateTbClue(clue);

        //添加线索跟进记录
        clueTrackRecordMapper.addClueTrackRecord(clueTrackRecord);

    }

    @Override
    public List<TbClueTrackRecord> list(Long clueId) {
        return clueTrackRecordMapper.selectList(clueId);
    }
}
